/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-10-12
 * Time: 20:35
 */
public class MyCircularQueue {
    public int[] elem;
    public  int front;//队头下标
    public  int rear;//队尾下标
    public MyCircularQueue(int k) {
        this.elem = new int[k];
    }

    //入队列
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        this.elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }
    //出队列
    public boolean deQueue() {
        if (isEmpty()){
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }
    //得到队头元素函数
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.elem[front];
    }
    //得到队尾元素
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int pos = (rear == 0) ? elem.length - 1 : rear - 1;
        return this.elem[pos];
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}
